
#include <stdio.h>
#include <string.h>

#include "bocat/common/md5.hpp"

#include "nmaper.hpp"

void	test_md5sum( void )
{
	fprintf( stdout, "===== %s =====\n", __func__ );

	{
		const char*	s = "hello";
		size_t	len = strlen( s );

		bocat::common::MD5	m;
		m.update( s, len );
		fprintf( stderr, "    MD5SUM '%s' ==> %s (len=%lu)\n", s, m.hexdigest(), len );

		{
			int	bits = 0;
			NMaper*	map_p = new Md5sumMaper(16, 1, bits);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 8;
			NMaper*	map_p = new Md5sumMaper(16, 1, bits);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 16;
			NMaper*	map_p = new Md5sumMaper(16, 1, bits);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5sumMaper(256, 1);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5sumMaper(4096, 1);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5sumMaper(65536, 1);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			int	bits = 0;
			NMaper*	map_p = new Md5sumMaper(1048576, 1, bits);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 24;
			NMaper*	map_p = new Md5sumMaper(1048576, 1, bits);

			int	id = map_p->map( s, len );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
	}

	fprintf( stdout, "----- %s -----\n", __func__ );
}



void	test_md5bin( void )
{
	fprintf( stdout, "===== %s =====\n", __func__ );

	{
		const char*	s = "hello";
		size_t	len = strlen( s );

		bocat::common::MD5	m;
		m.update( s, len );
		fprintf( stderr, "    MD5SUM '%s' ==> %s (len=%lu)\n", s, m.hexdigest(), len );

		{
			int	bits = 0;
			NMaper*	map_p = new Md5binMaper(16, 1, bits);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 8;
			NMaper*	map_p = new Md5binMaper(16, 1, bits);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 16;
			NMaper*	map_p = new Md5binMaper(16, 1, bits);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5binMaper(256, 1);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5binMaper(4096, 1);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5binMaper(65536, 1);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			int	bits = 0;
			NMaper*	map_p = new Md5binMaper(1048576, 1, bits);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 24;
			NMaper*	map_p = new Md5binMaper(1048576, 1, bits);

			int	id = map_p->map( m.digest(), 16 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
	}

	fprintf( stdout, "----- %s -----\n", __func__ );
}


void	test_md5hex( void )
{
	fprintf( stdout, "===== %s =====\n", __func__ );

	{
		const char*	s = "hello";
		size_t	len = strlen( s );

		bocat::common::MD5	m;
		m.update( s, len );
		fprintf( stderr, "    MD5SUM '%s' ==> %s (len=%lu)\n", s, m.hexdigest(), len );

		{
			int	bits = 0;
			NMaper*	map_p = new Md5hexMaper(16, 1, bits);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 8;
			NMaper*	map_p = new Md5hexMaper(16, 1, bits);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 16;
			NMaper*	map_p = new Md5hexMaper(16, 1, bits);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5hexMaper(256, 1);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5hexMaper(4096, 1);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			NMaper*	map_p = new Md5hexMaper(65536, 1);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d)\n", s, id, id, map_p->size() );
			delete map_p;
		}
		{
			int	bits = 0;
			NMaper*	map_p = new Md5hexMaper(1048576, 1, bits);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
		{
			int	bits = 24;
			NMaper*	map_p = new Md5hexMaper(1048576, 1, bits);

			int	id = map_p->map( m.hexdigest(), 32 );
			fprintf( stderr, "    Map    '%s' ==> %8d (0x%-8x) (MapSize:%d bits:%d)\n", s, id, id, map_p->size(), bits );
			delete map_p;
		}
	}

	fprintf( stdout, "----- %s -----\n", __func__ );
}



int	main( void )
{
	test_md5sum();
	test_md5bin();
	test_md5hex();

	return	0;
}

